﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SimpleCrawler
{
    public partial class Form3 : Form
    {
        public Form3(Form1 rootForm)
        {
            InitializeComponent();

            // calculate domain
            Dictionary<string, int> domains = new Dictionary<string, int>();
            for (int i = 0; i < rootForm.totalUrls.Count; i++)
            {
                int s = rootForm.totalUrls[i].IndexOf("//");
                if (s == -1) continue;
                s += 2;
                int e = rootForm.totalUrls[i].IndexOf("/", s);
                if (e == -1) e = rootForm.totalUrls[i].Length;
                string domain = rootForm.totalUrls[i].Substring(s, e - s);
                if (domains.ContainsKey(domain))
                    domains[domain]++;
                else
                    domains.Add(domain, 1);
            }

            List<KeyValuePair<string, int>> sortList = domains.ToList();
            sortList.Sort((firstPair, nextPair) =>
            {
                return firstPair.Value.CompareTo(nextPair.Value);
            }
            );

            for (int i = sortList.Count - 1; i >= 0; i--)
            {
                richTextBox1.AppendText(sortList[i].Value + " " + sortList[i].Key + "\n");
            }
        }
    }
}
